//
//  NewFeatureCollectionViewController.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/8.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit
import SnapKit
//可重用ID
private let WBNewFeatureViewCellID = "WBNewFeatureViewCellID"
//新特性图片数量
private let WBNewFeatureImageCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {
    
    init() {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
      //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        //注册可重用cell
        collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellID)
        

    }
    
    //Mark: - 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBNewFeatureImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellID, forIndexPath: indexPath) as! NewFeatureCell
        
//        cell.backgroundColor = UIColor.redColor()
        cell.imageIndex = indexPath.item
        
        return cell
        
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let page = Int (scrollView.contentOffset.x / scrollView.bounds.width)
        
        if page != WBNewFeatureImageCount - 1 {
            
            return
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        cell.showButtonAnim()
    }

   
}

private class NewFeatureCell: UICollectionViewCell {
    ///图像属性
    private var imageIndex: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true

        }
        
    }
    //按钮动画
    func showButtonAnim() {
        
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.6,
            delay: 0,
            usingSpringWithDamping: 0.7,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
               self.startButton.userInteractionEnabled = true
        }
    }
    //按钮监听方法
    @objc private func clickStartButton() {
//        print("开始体验")
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    func setupUI() {
        
        addSubview(iconView)
        addSubview(startButton)
        
        iconView.frame = bounds
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        startButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
        
    }
  
    
    //懒加载图片
    private lazy var iconView: UIImageView = UIImageView()
    //懒加载按钮
    private lazy var startButton: UIButton = UIButton(title: "开始体验", backImageName: "new_feature_finish_button", color: UIColor.whiteColor())
}
